import baiduAPI
import execlHandler


def exec(text):
    b = baiduAPI.emotion(text)
    isHate = baiduAPI.hate(text)
    confidence = b['confidence']
    negative_prob = b['negative_prob']
    positive_prob = b['positive_prob']
    sentiment = b['sentiment']
    return [isHate, confidence, negative_prob, positive_prob, sentiment]


def handelEXCEL(begin, end):
    for i in range(begin, end + 1):
        if i == 1:
            continue
        text = execlHandler.accessor.read_cell(i, 'A')  # 读取第i行A列的值
        if text == None:
            break
        result = exec(text)
        execlHandler.accessor.write_cell(i, 'B', result[0])
        execlHandler.accessor.write_cell(i, 'C', result[1])
        execlHandler.accessor.write_cell(i, 'D', result[2])
        execlHandler.accessor.write_cell(i, 'E', result[3])
        execlHandler.accessor.write_cell(i, 'F', result[4])


flag = input("处理表格键入1，处理单独文本输入2 —— ")
if (flag == '1'):
    begin = int(input("输入开始的行数："))
    end = int(input("输入结束的行数："))
    print("处理表格ing")
    handelEXCEL(begin, end)
else:
    text = input("输入处理内容：")
    print("处理ing")
    arr = exec(text)
    print(arr[0])
    print("主观程度：", arr[1])
    print("消极程度：", arr[2])
    print("积极程度：", arr[3])
    print("情绪：", arr[4])
